#pragma once 
#include <unistd.h>
#include <iostream>
#include <string>
#include <queue>

using namespace std;
#define CAPACITY 1024

class MsgPool
{
    public:
        MsgPool(size_t capa = CAPACITY)
        {
            pthread_mutex_init(&lock_vec_,NULL);
            pthread_cond_init(&cond_con_,NULL);
            pthread_cond_init(&cond_pro_,NULL);
            capacity_ = capa;
        }
        ~MsgPool()
        {
            pthread_mutex_destroy(&lock_vec_);
            pthread_cond_destroy(&cond_con_);
            pthread_cond_destroy(&cond_pro_);
        }

        void PushMsg(const string& msg)
        {
            pthread_mutex_lock(&lock_vec_);
            while(vec_.size() >= capacity_)
            {   
                pthread_cond_wait(&cond_pro_,&lock_vec_);
            }
            vec_.push(msg);
            pthread_mutex_unlock(&lock_vec_);
            pthread_cond_signal(&cond_con_);
        }

        void PopMsg(const string& msg)
        {
            pthread_mutex_lock(&lock_vec_);
            while(vec_.empty())
            {
                pthread_cond_wait(&cond_con_,&lock_vec_);
            }
            pthread_mutex_unlock(&lock_vec_);
            pthread_cond_signal(&cond_pro_);
        }
    private:
        queue<string> vec_;//保存消息的容器
        size_t capacity_;//定义容器的容量
        pthread_mutex_t lock_vec_;//保护消息容器的锁
        pthread_cond_t cond_con_;//消费者的条件变量
        pthread_cond_t cond_pro_;//生产者的条件变量

};

